﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml.Linq;
using IKE.LCAFormat.ILCD.Common;

namespace IKE.LCAFormat.ILCD.Flow
{
    public class FlowDataSet
    {
        public static IEnumerable<FlowDataSet> Allflow(string path)
        {
            //查找指定目录下所有XML文件
            string[] files = Directory.GetFiles(path, "*.xml");
            foreach (var file in files)
            {
                yield return FlowDataSet.Load(file);
            }
        }
        /// <summary>
        /// 根据xml文件路径读取XML
        /// </summary>
        /// <param name="xmlfile">xml文件路径</param>
        /// <param name="withoutExchanges"></param>
        /// <returns></returns>
        private static FlowDataSet Load(string xmlfile)
        {
            if (File.Exists(xmlfile))
            {

                XDocument doc = XDocument.Load(xmlfile);
                return FlowDataSet.Create(doc);
            }
            else
                return new FlowDataSet();
        }
        /// <summary>
        /// 在XML文档中创建FlowDataSet
        /// </summary>
        /// <param name="doc">xml文档</param>
        /// <returns></returns>
        public static FlowDataSet Create(XDocument doc)
        {
            XNamespace flow = "http://lca.jrc.it/ILCD/Flow";
            XNamespace common = "http://lca.jrc.it/ILCD/Common";
            AutoSerializeBase.DictNamespaces["Flow"] = flow;
            AutoSerializeBase.DictNamespaces["common"] = common;

            FlowDataSet ds = new FlowDataSet();
            var root = doc.Element(flow + "flowDataSet");
            ds.flowInformation.FromElement(root.Element(flow+"flowInformation"));
            ds.modellingAndValidation.FromElement(root.Element( flow+"modellingAndValidation"));
            ds.administrativeInformation.FromElement(root.Element(flow+"administrativeInformation"));
            ds.flowProperties.FromElement(root.Element(flow+"flowProperties"));
            return ds;
        }
        /// <summary>
        /// 根据xml字符串读取XML
        /// </summary>
        /// <param name="xmlStr">xml字符串</param>
        /// <returns></returns>
         public static FlowDataSet Parse(string xmlStr,bool withoutExchanges)
        {
            XDocument doc = XDocument.Parse(xmlStr);
            return FlowDataSet.Create(doc);
        }

        public void Save(string xmlFile)
        {
            this.Populate().Save(xmlFile);
        }

        public void Save(System.IO.TextWriter tw )
        {
            this.Populate().Save(tw);
        }

        public void Save(System.Xml.XmlWriter xmlWriter)
        {
            this.Populate().Save(xmlWriter);
        }
         public XDocument Populate()
        {
            XDocument doc = new XDocument();
            doc.Add(new XProcessingInstruction("xml-stylesheet", "version=\"1.0\" type=\"text/xsl\" href=\"../../stylesheets/flow2html.xsl\""));

            XNamespace flow = "http://lca.jrc.it/ILCD/Flow";
            XNamespace common = "http://lca.jrc.it/ILCD/Common";
            XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
            XElement root = new XElement(flow + "flowDataSet",
                new XAttribute("xmlns", "http://lca.jrc.it/ILCD/Flow"),
                new XAttribute(XNamespace.Xmlns + "common", "http://lca.jrc.it/ILCD/Common"),
                new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                new XAttribute(xsi + "schemaLocation", "http://lca.jrc.it/ILCD/Flow ../../schemas/ILCD_FlowDataSet.xsd"),
                new XAttribute("locations", "../ILCDLocations.xml"),
                new XAttribute("version","1.1")
            );
            AutoSerializeBase.DictNamespaces["Flow"] = flow;
            AutoSerializeBase.DictNamespaces["common"] = common;
            root.Add(flowInformation.ToElement(flow + "flowInformation"));
            root.Add(modellingAndValidation.ToElement(flow + "modellingAndValidation"));
            root.Add(administrativeInformation.ToElement(flow + "administrativeInformation"));
            root.Add(flowProperties.ToElement(flow + "flowProperties"));
            doc.Add(root);
            return doc;
        }
        [DisplayUI(1)]
         public FlowInformationType flowInformation = new FlowInformationType();

        [DisplayUI(2)]
        public ModellingAndValidationType modellingAndValidation = new ModellingAndValidationType();


        [DisplayUI(3)]
        public AdministrativeInformationType administrativeInformation = new AdministrativeInformationType();
        [DisplayUI(4)]
        public FlowPropertiesType flowProperties = new FlowPropertiesType();
        //[DisplayUI(5)]
        //public ExchangesType exchanges = new ExchangesType();
       
        
    }
}
